package view
{
	//import com.gskinner.motion.GTween;
	import com.gskinner.motion.GTweener;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.*;
	
	import flashx.textLayout.formats.BackgroundColor;
	
	import model.*;
	
	import org.osmf.events.TimeEvent;
	
	public class GameView extends Sprite
	{ 
		private var display:Display = new Display();
		private var settingDialog:SettingDialog = new SettingDialog();
		public var game:Game = new Game();
		
		private var targetShape:Sprite = new Sprite();
		private var isPlaying:Boolean = false;
		
		//text på displayen
		private var missText:TextField = display.getChildByName("miss_text") as TextField;
		private var misses:int;
		
		private var leftText:TextField = display.getChildByName("left_text") as TextField;
		private var bricksLeft:int = 0;
		
		private var timeText:TextField = display.getChildByName("time_text") as TextField;
		private var time:Number;
		//private var elapsedTime:Number;
		private var timer:Timer = new Timer(100);;
		private var timeOnStart:Number = 0;
		
		public function GameView(aGame:Game)
		{
			game = aGame;
			//timer.addEventListener(TimerEvent.TIMER, onTimer);
		}
		
		/**
		 * *******************DRAW FUNKTIONER**********************
		 **/

		public function DrawAll():void
		{
			AddBg();
			AddTopDisplay();
			
			if(isPlaying == true)
			{
				DrawGamePlan();
				DrawTargetBrick();
			}

			AddSettingDialog();
		}
		
		public function AddBg():void
		{
			var bg:Shape = new Shape();
			bg.graphics.beginFill(0x313131);
			bg.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight);
			this.addChild(bg);
		}
		
		public function AddTopDisplay():void
		{
			this.addChild(display);
		}
		
		//lägger till menu
		public function AddSettingDialog():void
		{
			settingDialog.y = 350;
			
			var startBtn:Sprite = settingDialog.getChildByName("startBtn") as Sprite;
			var stopBtn:Sprite = settingDialog.getChildByName("stop_btn") as Sprite;
			
			var configBtn:Sprite = settingDialog.getChildByName("configBtn") as Sprite;
			var changeBtn:Sprite = settingDialog.getChildByName("changeBtn") as Sprite;
			
			var btn16:Sprite = settingDialog.getChildByName("btn16") as Sprite;
			var btn64:Sprite = settingDialog.getChildByName("btn64") as Sprite;
			var btn256:Sprite = settingDialog.getChildByName("btn256") as Sprite;
			
			this.addChild(settingDialog);
			
			if(isPlaying)//spelar
			{
				//tylko stop jest active
				startBtn.buttonMode = false;
				startBtn.alpha = 0.4;
				startBtn.removeEventListener(MouseEvent.CLICK, onStartButton);
				
				stopBtn.buttonMode = true;
				stopBtn.alpha = 1;
				stopBtn.addEventListener(MouseEvent.CLICK, onStopButton);
				
				configBtn.buttonMode = false;	
				configBtn.alpha = 0.4;
				configBtn.removeEventListener(MouseEvent.CLICK, onConfigButton);
			}
			else//inte spelar
			{
				startBtn.buttonMode = true;
				startBtn.alpha = 1;
				startBtn.addEventListener(MouseEvent.CLICK, onStartButton);
				
				stopBtn.buttonMode = false;
				stopBtn.alpha = 0.4;
				stopBtn.removeEventListener(MouseEvent.CLICK, onStopButton);
				
				configBtn.buttonMode = true;	
				configBtn.alpha = 1;
				configBtn.addEventListener(MouseEvent.CLICK, onConfigButton);
			}
		}
		
		//ritar ut spelPlanet utifrån game arrayens innehåll
		public function DrawGamePlan():void
		{
			var brickSize:int = stage.stageWidth/game.getPlanSide();
			var leftMargin:int = 0;
			var topMargin:int = 50;
			var count:int = 0;
			var brickArray:Array = game.getBricks();
			
			//kollar vilken color som brickan ska ha 
			//samt dess namn för att jämföra med den som man har klickat på
			for each(var brick:IBrick in brickArray)
			{
				var brickSprite:Sprite = new Sprite();
				
				if(brick.getColor() == game.RED)
				{
					brickSprite.graphics.lineStyle(1, 0x000000);
					brickSprite.graphics.beginFill(0xFF0000);
					brickSprite.name = game.RED.toString();
				}
				else if(brick.getColor() == game.GREEN)
				{
					brickSprite.graphics.lineStyle(1, 0x000000);
					brickSprite.graphics.beginFill(0x00FF00);
					brickSprite.name = game.GREEN.toString();
				}
				else if(brick.getColor() == game.BLUE)
				{
					brickSprite.graphics.lineStyle(1, 0x000000);
					brickSprite.graphics.beginFill(0x0000FF);
					brickSprite.name = game.BLUE.toString();
				}
				else
				{
					brickSprite.graphics.lineStyle(1, 0x000000);
					brickSprite.graphics.beginFill(0xFFFF00);
					brickSprite.name = game.YELLOW.toString();
				}
				
				if(count >= game.getPlanSide())
				{
					leftMargin = 0;
					//horizontal-vågrätt forflyttning
					topMargin = brickSize + topMargin+1;
					count = 0;
				}
				
				//kollar upp vilken form som brickan ska ha
				if(game.currentBrickType == game.SQUARE_BRICK)
				{
					brickSprite.graphics.drawRect(leftMargin, topMargin, brickSize, brickSize);
				}
				else
				{
					brickSprite.graphics.drawCircle(leftMargin+brickSize/2, topMargin+brickSize/2, brickSize/2);
				}
				
				count++;
				//Lodrätt forflyttning
				leftMargin = leftMargin + brickSize+1;
				
				brickSprite.addEventListener(MouseEvent.CLICK, onBrickClicked);
				
				this.addChild(brickSprite);

			}
		}
		
		public function DrawTargetBrick():void
		{
			var targetBrick:SquareBrick = new SquareBrick();
			targetBrick.setColor(game.getTargetColor());
			
			targetShape.graphics.clear();
			
			//sätter färg
			if(targetBrick.getColor() == game.RED)
			{
				targetShape.graphics.lineStyle(1, 0xffffff);
				targetShape.graphics.beginFill(0xFF0000);	
			}
			else if(targetBrick.getColor() == game.GREEN)
			{
				targetShape.graphics.lineStyle(1, 0xffffff);
				targetShape.graphics.beginFill(0x00FF00);	
			}
			else if(targetBrick.getColor() == game.BLUE)
			{
				targetShape.graphics.lineStyle(1, 0xffffff);
				targetShape.graphics.beginFill(0x0000FF);	
			}
			else
			{
				targetShape.graphics.lineStyle(1, 0xffffff);
				targetShape.graphics.beginFill(0xFFFF00);	
			}
			
			//sätter form
			if(game.currentBrickType == game.SQUARE_BRICK)
			{
				targetShape.graphics.drawRect(170,15,20,20);
			}
			else
			{
				targetShape.graphics.drawCircle(170, 25, 10);
			}
			
			this.addChild(targetShape);
			
		}
		
		
		/**
		 * *******************EVENT FUNKTIONER**********************
		 **/
		
		public function onBrickClicked(e:MouseEvent):void
		{
			var brick:Sprite = e.target as Sprite;
			
			if(isPlaying)
			{
				if(game.targetColor.toString() == brick.name)
				{
					brick.alpha = 0.1;
					bricksLeft--;
					leftText.text = bricksLeft.toString();
					brick.removeEventListener(MouseEvent.CLICK, onBrickClicked);
					
					if(bricksLeft == 0)
					{
						timer.stop();
						isPlaying = false;
						missText.text = misses.toString();
						AddSettingDialog();
						
						//zrob removeListener
					}
				}
				else
				{
					misses++;
					missText.text = misses.toString();
				}
				
					
			}
		}
		
		public function onStartButton(e:MouseEvent):void
		{
			isPlaying = true;
			
			//timer = new Timer(100);
			
			timer.addEventListener(TimerEvent.TIMER, onTimer);
			timer.start();
			game.RestartBricks();
			
			bricksLeft = (game.getPlanSide() * game.getPlanSide())/4;
			leftText.text = bricksLeft.toString();
			misses = 0;
			missText.text = misses.toString();
			
			DrawAll();
		}
		
		private function onTimer(e:TimerEvent):void
		{
			if(isPlaying)
			{
				time = (getTimer() - timeOnStart) / 1000 + misses;
				timeText.text = time.toFixed(1) + "0";
			}
		}
		private function onStopButton(e:MouseEvent):void
		{
			isPlaying = false;
			timer.stop();
			timer = null;
			AddSettingDialog();
			
		}
		private function onConfigButton(e:MouseEvent):void
		{
			//if(settingDialog.y == 350)
			//{
				//new GTween(settingDialog, 0.5, {y : 0});
			//}
			//else
			//{
				//new GTween(settingDialog, 0.5, {y : 350});
			//}
		}
		private function on16Button(e:MouseEvent):void
		{
			
		}
		private function on64Button(e:MouseEvent):void
		{
			
		}
		private function on256Button(e:MouseEvent):void
		{
			
		}
		private function onChangeButton(e:MouseEvent):void
		{
			
		}
		/*if (mSettingsDialog.y == 350)
		{
			new GTween(mSettingsDialog, 0.5, {y : 0});
		}
		else
		{
			new GTween(mSettingsDialog, 0.5, {y : 350});
		}*/
		
		
	}
}